<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="name" :label="$t('menu.name')">
        <el-input v-model="dataForm.name" :placeholder="$t('menu.name')"></el-input>
      </el-form-item>
      <el-form-item prop="parentName" :label="$t('menu.parentName')" class="menu-list">
        <!--父级选择框-->
        <uni-tree-select v-model="dataForm.pid" ref="parentTree" :data="parentTreeData" />
      </el-form-item>
      <el-form-item prop="sort" :label="$t('menu.sort')">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" :label="$t('menu.sort')">
        </el-input-number>
      </el-form-item>
      <el-form-item v-if="[1].indexOf(dataForm.type) === -1" prop="icon" :label="$t('menu.icon')" class="icon-list">
        <el-popover v-model="iconListVisible" ref="iconListPopover" placement="bottom-end" trigger="click"
                    popper-class="mod-sys__menu-icon-popover">
          <div class="mod-sys__menu-icon-inner">
            <div class="mod-sys__menu-icon-list">
              <el-button v-for="(item, index) in iconList" :key="index" @click="iconListCurrentChangeHandle(item)"
                         :class="{ 'is-active': dataForm.icon === item }">
                <svg class="icon-svg" aria-hidden="true">
                  <use :xlink:href="`#${item}`"></use>
                </svg>
              </el-button>
            </div>
          </div>
        </el-popover>
        <el-input v-model="dataForm.icon" :readonly="false" :placeholder="$t('menu.icon')">
          <el-button slot="append" icon="el-icon-search" v-popover:iconListPopover></el-button>
        </el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { getIconList, treeDataTranslate } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      menuListVisible: false,
      iconList: [],
      iconListVisible: false,
      parentTreeData: [],
      dataForm: {
        id: '',
        sourceCode: '',
        tableName: '',
        tableId: 0,
        name: '',
        pid: '0',
        icon: ''
      },
      pids: {}
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.iconList = getIconList()
        this.dataForm.parentName = this.$t('menu.parentNameDefault')
        this.getMenuList().then(() => {
          this.getSortInfo()
        })
      })
    },
    getSortInfo () {
      this.$http.get(`/sys/menu/getMenuSort`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dataForm.sort = res.data.sort
      }).catch(() => { })
    },
    // 获取菜单列表
    getMenuList () {
      return this.$http.get('/sys/menu/list?type=0').then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        res.data.push({ id: 0, pid: 0, name: this.$t('menu.parentNameDefault') })
        this.parentTreeData = treeDataTranslate(res.data, 'id', 'pid')
      }).catch(() => { })
    },
    // 上级菜单树, 设置默认值
    deptListTreeSetDefaultHandle () {
      this.dataForm.pid = '0'
      this.dataForm.parentName = this.$t('menu.parentNameDefault')
    },
    // 图标, 选中
    iconListCurrentChangeHandle (icon) {
      this.dataForm.icon = icon
      this.iconListVisible = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.post('/gen/genTable/geneMenu', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss">
  .mod-sys__menu {
    .menu-list,
    .icon-list {
      .el-input__inner,
      .el-input__suffix {
        cursor: pointer;
      }
    }

    &-icon-popover {
      width: 458px;
      overflow: hidden;
    }

    &-icon-inner {
      width: 478px;
      max-height: 258px;
      overflow-x: hidden;
      overflow-y: auto;
    }

    &-icon-list {
      width: 458px;
      padding: 0;
      margin: -8px 0 0 -8px;

      > .el-button {
        padding: 8px;
        margin: 8px 0 0 8px;

        > span {
          display: inline-block;
          vertical-align: middle;
          width: 18px;
          height: 18px;
          font-size: 18px;
        }
      }
    }
  }
</style>
